package com.yiyahui.project.erp.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.yiyahui.common.utils.poi.ExcelUtil;
import com.yiyahui.framework.aspectj.lang.annotation.Log;
import com.yiyahui.framework.aspectj.lang.enums.BusinessType;
import com.yiyahui.framework.web.controller.BaseController;
import com.yiyahui.framework.web.domain.AjaxResult;
import com.yiyahui.framework.web.domain.BaseEntity;
import com.yiyahui.framework.web.page.TableDataInfo;
import com.yiyahui.project.erp.domain.YiyaAppointment;
import com.yiyahui.project.erp.domain.YiyaClinicPerson;
import com.yiyahui.project.erp.domain.requestVo.YiyaAppointmentRequest;
import com.yiyahui.project.erp.domain.responseVo.YiyaAppointmentAndPersonVo;
import com.yiyahui.project.erp.service.IYiyaAppointmentService;
import com.yiyahui.project.erp.service.IYiyaClinicPersonService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 预约Controller
 * 
 * @author yiyahui
 * @date 2020-05-19
 */
@Api("预约")
@RestController
@RequestMapping("/erp/appointment")
public class YiyaAppointmentController extends BaseController
{
    @Autowired
    private IYiyaAppointmentService yiyaAppointmentService;

    @Autowired
    private IYiyaClinicPersonService yiyaClinicPersonService;
 
    /**
     * 查询预约列表
     */
    @ApiOperation("查询预约列表")
    @PreAuthorize("@ss.hasPermi('erp:appointment:list')")
    @GetMapping("/list")
    public TableDataInfo list(YiyaAppointmentRequest yiyaAppointmentRequest)
    {
        startPage();
        List<YiyaAppointment> list = yiyaAppointmentService.selectYiyaAppointmentList(yiyaAppointmentRequest);
        return getDataTable(list);
    }

    /**
     * 导出预约列表
     */
    @ApiOperation("导出预约列表")
    @PreAuthorize("@ss.hasPermi('erp:appointment:export')")
    @Log(title = "预约", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(YiyaAppointmentRequest yiyaAppointmentRequest)
    {
        List<YiyaAppointment> list = yiyaAppointmentService.selectYiyaAppointmentList(yiyaAppointmentRequest);
        ExcelUtil<YiyaAppointment> util = new ExcelUtil<YiyaAppointment>(YiyaAppointment.class);
        return util.exportExcel(list, "appointment");
    }

    /**
     * 获取预约详细信息
     */
    @ApiOperation("获取预约详细信息")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:appointment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yiyaAppointmentService.selectYiyaAppointmentById(id));
    }

    /**
     * 新增预约
     */
    @ApiOperation("新增预约")
    @ApiImplicitParam(name = "yiyaAppointment", value = "${tableComment}", dataType = "YiyaAppointment")
    @PreAuthorize("@ss.hasPermi('erp:appointment:add')")
    @Log(title = "预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YiyaAppointment yiyaAppointment)
    {
        return toAjax(yiyaAppointmentService.insertYiyaAppointment(yiyaAppointment));
    }

    /**
     * 修改预约
     */
    @ApiOperation("修改预约")
    @ApiImplicitParam(name = "yiyaAppointment", value = "${tableComment}", dataType = "YiyaAppointment")
    @PreAuthorize("@ss.hasPermi('erp:appointment:edit')")
    @Log(title = "预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YiyaAppointment yiyaAppointment)
    {
        return toAjax(yiyaAppointmentService.updateYiyaAppointment(yiyaAppointment));
    }

    /**
     * 删除预约
     */
    @ApiOperation("删除预约")
    @ApiImplicitParam(name = "ids", value = "主键id数组", required = true, dataType = "Long[]", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:appointment:remove')")
    @Log(title = "预约", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yiyaAppointmentService.deleteYiyaAppointmentByIds(ids));
    }

    /**
     * 全部预约列表详情
     */
    @ApiOperation("全部预约列表详情")
    @PostMapping("/list/all")
    public TableDataInfo listAppointmentAndPerson(@RequestBody YiyaAppointmentRequest yiyaAppointmentRequest)
    {
        startPageByPost(yiyaAppointmentRequest);
        List<YiyaAppointment> list = yiyaAppointmentService.selectYiyaAppointmentList(yiyaAppointmentRequest);
        List<Long> pIds = list.stream().map(YiyaAppointment::getClinicPersonId).distinct().collect(Collectors.toList());
        Map<Long, YiyaClinicPerson> map = yiyaClinicPersonService.selectByIds(pIds).stream().collect(Collectors.toMap(YiyaClinicPerson::getId, a -> a, (k1, k2) -> k1));
        List<YiyaAppointmentAndPersonVo> yiyaAppointmentAndPersonVos = new ArrayList<>();
        list.forEach(r -> {
            YiyaAppointmentAndPersonVo yiyaAppointmentAndPersonVo = new YiyaAppointmentAndPersonVo();
            yiyaAppointmentAndPersonVo.setYiyaAppointment(r);
            yiyaAppointmentAndPersonVo.setYiyaClinicPerson(map.get(r.getClinicPersonId()));
            yiyaAppointmentAndPersonVos.add(yiyaAppointmentAndPersonVo);
        });
        return getDataTable(yiyaAppointmentAndPersonVos,list);
    }
}
